package com.songkun.template.viewmodel;

import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.songkun.template.R;
import com.songkun.template.databinding.ActivityBaseBinding;
import com.songkun.template.utils.UIUtils;


/**
 * @描述: 通用浏览器的webview
 * @项目名: AndroidTemplate
 * @包名: com.songkun.template.viewmodel
 * @类名:
 * @作者: soongkun
 * @创建时间: 2016/4/29 13:16
 */

public class BaseWebViewViewModel {

    private final ActivityBaseBinding mBinding;
    private String mTitle;
    private WebView mWebView;


    public BaseWebViewViewModel(ActivityBaseBinding binding) {
        this.mBinding = binding;
    }


    public void initWebView(WebView webView) {
        mWebView = webView;

        WebSettings webSettings = mWebView.getSettings();

        webSettings.setSupportZoom(false); // 不支持缩放

        webSettings.setJavaScriptEnabled(true);// 开启Javascript支持

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);

        mWebView.setBackgroundColor(UIUtils.getColor(R.color.white));  //  WebView 背景透明效果

        //添加客户端支持
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

                if(mTitle == null){
                    mBinding.toolbar.setTitle(title);
                }
            }
        });

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                return false;

            }
        });
    }


    public void loadUrl(String pageurl) {
        mWebView.loadUrl(pageurl);
    }


    public void setTitle(String title) {
        mTitle = title;
    }
}
